<?php
namespace App\Http\Controllers\Admin\Ywuser;

use App\Service\Admin\Ywuser\TmpService;
use App\Utility\Upload;
use Illuminate\Http\Request;

class TmpController extends BaseController
{
    //物品

    //列表
    public function index(Request $request)
    {
        $res = TmpService::index();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //选项
    public function option(Request $request)
    {
        $res = TmpService::option();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function modify(Request $request)
    {
        $id = $request->post('id', 0);
        $uname = $request->post('uname', '');
        $pas['cate_id'] = $request->post('cate_id', 0);
        $pas['title'] = $request->post('title', '');
        $pas['describe'] = $request->post('describe', '');
        $pas['code'] = $request->post('code', '');
        $icon = $request->post('img', '');
        if ($icon) {
            if (substr($icon, 0, 5) == 'data:') {
                $pas['thumb'] = Upload::getPathByBase64($icon);
            } else {
                $pas['thumb'] = $icon;
            }
        }
        $pas['uid'] = TmpService::uidByUname($uname);
        if ($id) {
            $res = TmpService::modify($id, $pas);
        } else {
            $res = TmpService::add($pas);
        }
        if ($res['code']!=0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        $res = TmpService::del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}